package com.derive.education_service.web.user.controller;

import com.derive.education_service.common.global.Paper;
import com.derive.education_service.common.global.ResponseMessage;
import com.derive.education_service.entity.User;
import com.derive.education_service.web.user.bean.dto.Info;
import com.derive.education_service.web.user.bean.dto.QueryParam;
import com.derive.education_service.web.user.bean.dto.UserAddDto;
import com.derive.education_service.web.user.bean.dto.UserUpDto;
import com.derive.education_service.web.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 用户操作接口
 */
@RestController
@Slf4j
@RequestMapping("user")
@Validated
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param userAddDto
     * @return
     */
    @PostMapping("addUserInfo")
    public ResponseMessage addUserInfo(@Validated @RequestBody UserAddDto userAddDto) {
        boolean bol = userService.addUserInfo(userAddDto);
        if (bol) {
            return ResponseMessage.createBySuccessMessage("用户注册成功");
        } else {
            return ResponseMessage.createByErrorMessage("用户注册异常");
        }
    }

    /**
     * 查看客户详细信息 -- 根据openId
     * @param openId
     * @return
     */
    @GetMapping("getUserInfoByOpenId")
    public ResponseMessage getUserInfoByOpenId(@NotBlank(message = "用户openId为必填项") String openId) {
        return userService.getUserInfoByOpenId(openId);
    }

    /**
     * 更新客户基本信息
     * 完善用户基本信息
     * @param userUpDto
     * @return
     */
    @PostMapping("upUserInfoById")
    public ResponseMessage upUserInfoById(@Validated @RequestBody UserUpDto userUpDto) {
        boolean bol = userService.upUserInfoById(userUpDto);
        if (bol) {
            return ResponseMessage.createBySuccessMessage("用户信息更新成功");
        } else {
            return ResponseMessage.createByErrorMessage("用户更新异常");
        }
    }

    /**
     * 删除客户信息 -- 关联信息
     * 更改用户状态为失效即可
     * @param userId
     * @return
     */
    @PostMapping("delUserById")
    public ResponseMessage delUserById(@Validated @NotNull Integer userId) {
        boolean bol = userService.delUserById(userId);
        if (bol) {
            return ResponseMessage.createBySuccessMessage("用户删除成功");
        } else {
            return ResponseMessage.createByErrorMessage("用户删除异常");
        }
    }

    /**
     * 检索客户列表信息 -- 分页查询
     * 根据名字模糊查询、学历、用户状态(默认查正常)
     * @param queryParam
     * @return
     */
    @PostMapping("getUserList")
    public ResponseMessage getUserList(@RequestBody @Validated QueryParam queryParam) {
        Paper<User> userPaper = userService.getUserList(queryParam);
        return ResponseMessage.createBySuccess(userPaper);
    }

    /**
     * 更改用户标签
     * 目前使用简陋方式
     * 直接判断，简单粗暴
     * 设定为教学点负责人之后
     * 需要在代理管理中编辑上传负责人联系方式数据
     * @param userId
     * @param userTag
     * @return
     */
    @PostMapping("setUserTagById")
    private ResponseMessage setUserTagById(@NotNull(message = "用户id不能为空") int userId, @NotNull(message = "用户标签不能为空") int userTag) {
        return userService.setUserTagById(userId, userTag);
    }
}
